"""
!/usr/bin/env python
-*- coding: utf-8 -*-
@Author  : MarkJia
@Site    : 
@File    : contact_page.py
"""
import time

"""
通讯录页面对象
"""
from selenium.webdriver.common.by import By
from webtest.autowebui.page_object.base_page import BasePage
from webtest.autowebui.utils.Logger import logger


class ContactPage(BasePage):
    # 设置通讯录的base_url
    _base_url = 'https://work.weixin.qq.com/wework_admin/frame#contacts'

    # 提取出断言找的列表元素 ； 找到父级的第二个元素  ele:nth-child(2)
    _list_loc = (By.CSS_SELECTOR, ".member_colRight_memberTable_td:nth-child(2)")

    # 添加成员元素
    _add_member = (By.XPATH, "//div[@class='ww_operationBar']//a[text()='添加成员']")

    # + 号元素
    _jiahao_loc = (By.CSS_SELECTOR, ".member_colLeft_top_addBtn")

    # 添加部门元素
    _tianjiabumen_loc = (By.CSS_SELECTOR, ".js_create_party")

    # 输入部门信息元素
    _bumenxinxi_loc = (By.CSS_SELECTOR, "[type='text'][name='name']")

    # 点击选择所属部门
    _dianjixuanze_loc = (By.CSS_SELECTOR, ".js_parent_party_name")

    # 选择所属部门
    _xuanze_loc = (By.CSS_SELECTOR, ".member_colLeft.js_party_list_container .jstree-anchor")

    # 确定元素
    _queding_loc = (By.XPATH, "//*[text()='确定']")

    # 冒泡元素
    _maopao_loc = (By.CSS_SELECTOR, "#js_tips")

    # 部门列表元素
    _bumen_listloc = (By.CSS_SELECTOR, '.jstree-node')

    def goto_add_member(self):
        # 解决循环导入包报错问题 ： 出现 A -> B ，B -> A 循环导包的场景，需要把其中一个导包放到方法里面
        from webtest.autowebui.page_object.add_member_page import AddmemberPage
        """
        添加成员方法
        :return:
        """
        time.sleep(5)
        self.find_ele(self._add_member).click()
        return AddmemberPage(self.driver)

    # 返回测试用例需要断言的数据
    def get_list(self):
        """
        查看成员列表方法
        :return:
        """
        # 定位所有包含姓名列的 webdriver 对象
        web_object = self.find_eles(self._list_loc)
        name_list = [web_ele.text for web_ele in web_object]
        logger.info(f'添加成员信息获取成功后，获取的成员列表{name_list}')
        return name_list

    # 添加成员
    def add_member_in_contact(self):
        from webtest.autowebui.page_object.add_member_page import AddmemberPage
        self.wait_exist(self._add_member)
        self.wait_visible(self._add_member)
        self.wait_clickable(self._add_member)
        self.find_ele(self._add_member).click()
        time.sleep(5)
        return AddmemberPage(self.driver)

    # 添加部门
    def add_department(self, department_name):
        # 点击 + 号
        self.click(self._jiahao_loc)
        # 点击添加部门
        self.click(self._tianjiabumen_loc)
        # 输入部门基本信息
        self.input_(self._bumenxinxi_loc, department_name)
        # 点击选择所属部门
        self.click(self._dianjixuanze_loc)
        # 选择所属部门
        self.click(self._xuanze_loc)
        # 点击确定
        self.click(self._queding_loc)
        return ContactPage(self.driver)

    def department_assert_maopao(self):
        # 断言1 . 冒泡断言
        actual = self.find_ele(self._maopao_loc).text
        return actual

    def department_assert_list(self):
        # 断言 2 ，列表断言
        web_element = self.find_eles(self._bumen_listloc)
        web_list = [i.text for i in web_element]
        return web_list
